<?php
namespace app\common\model\orderdata;

use think\Model;
use app\common\model\user\User;
class StorageArea extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'storagearea';
    protected $pk    = 'StorageAreaId';

    const STATEOK  = 1; //1为选择生效的 目前为0 是因为全部 显示
    const STATEINT = 1;
    const STATENO  = 2;//暂停

    /**
     * 库存微调列表
     * @return [type] [description]
     */
    public static function getchangelist($id){
        if(empty($id)){
            return moerr('缺少参数');
        }
        $ids = StorageAreaGoods::where('StorageAreaId',$id)->column('StorageAreaGoodsId');
        $res['list'] = Storageareachange::where('StorageAreaGoodsId','in',$ids)->with(['area'])->order('StorageAreaChangeId desc')->select()->toArray();
        // dump($res['list']);
        foreach ($res['list'] as $key => $value) {
            $res['list'][$key]['GoodId'] = $value['area']['GoodId'];
        }
        //用户列表
        $userids = array_column($res['list'], 'UpUserId');
        $userlist = User::getpubliclist($userids);
        $userlist = arr_id_formate($userlist, 'UserId',true);
        $res['userlist'] = $userlist;
        //货物清单
        $goodids = array_column($res['list'], 'GoodId');
        $goodlist = Goods::getlist($goodids);
        $res['goodlist'] = arr_id_formate($goodlist,'GoodId',true);
        $res['areainfo'] = StorageArea::get($id)->hidden(['OperationLog']);
        return sok($res);
    }

    /**
     * 获取存储点列表
     * @return [type] [description]
     */
    public static function getlist($id = [])
    {
        $data = Storagearea::where('State', Storagearea::STATEOK);
        if (!empty($id)) {
            $data = $data->where('StorageAreaId', 'in', $id);
        }
        $data = $data->select();
        $data->hidden(['OperationLog']);
        return $data;
    }

    /**
     * 修改货物重量
     * @param  [type] $GoodIds [description]
     * @param  [type] $weights [description]
     * @param  string $type    [add sub]
     * @return [type]          [description]
     */
    public static function weight_change($GoodIds, $weights, $type = 'add')
    {
        if (!is_array($GoodIds)) {
            if (stripos($GoodIds, ',') !== false) {
                $GoodIds = explode($GoodIds, ',');
                $weights = explode($weights, ',');
            } else {
                $GoodIds = [$GoodIds];
                $weights = [$weights];
            }
        }
        //按照货物id从新调整数顺序
        $nweights = [];
        if ($type == 'add') {
            foreach ($GoodIds as $key => $value) {
                $nweights[$value] = $weights[$key];
            }
        } else if ($type == 'sub') {
            foreach ($GoodIds as $key => $value) {
                $nweights[$value] = -1 * $weights[$key];
            }
        }
        //检测货物是否足够减少
        $sb = Storagearea::where('GoodId', 'in', $GoodIds)->select();
        if ($type == 'sub') {
            foreach ($sb as $key => $data) {
                if (((empty($data->TotalWeight) ? 0 : $data->TotalWeight) + $nweights[$data->GoodId]) < 0) {
                    return serr($data->OrderNumber . '的库存重量不足以调整');
                }
            }
        }
        //调整库存
        foreach ($sb as $key => $data) {
            if (empty($data->TotalWeight)) {
                $data->TotalWeight = $nweights[$data->GoodId];
            } else {
                $data->TotalWeight = $data->TotalWeight + $nweights[$data->GoodId];
            }
            $data->save();
        }
        return sok();
    }

    
}
